import 'package:flutter/cupertino.dart';
import 'package:provider/provider.dart';
import 'package:short_video_app/entity/profile/follow_list_entity.dart';
import 'package:short_video_app/utils/color_util.dart';
import 'package:short_video_app/ui/widgets/custom_gesture.dart';
import 'package:short_video_app/ui/widgets/custom_network_image.dart';
import 'package:short_video_app/ui/widgets/custom_refresh.dart';
import 'package:short_video_app/ui/widgets/custom_scaffold.dart';
import 'package:short_video_app/ui/widgets/empty_page.dart';
import 'package:short_video_app/view_model/profile/follow_view_model.dart';

class FollowPage extends StatefulWidget {
  const FollowPage({Key? key}) : super(key: key);

  @override
  State<FollowPage> createState() => _FollowPageState();
}

class _FollowPageState extends State<FollowPage> {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (BuildContext context) => FollowViewModel(),
      child: Consumer<FollowViewModel>(
          builder: (context, viewModel, child) {
            return CustomScaffold(
              context: context,
              title: "关注",
              elevation: 0.2,
              child: CustomRefresh(
                controller: viewModel.refreshController,
                enablePullUp: true,
                onRefresh: () async {
                  viewModel.onRefresh();
                },
                onLoading: () async {
                  viewModel.onLoading();
                },
                child: _buildContent(viewModel),
              ),
            );
          }),
    );
  }

  Widget _buildContent(FollowViewModel viewModel) {
    return viewModel.datas.isEmpty
        ? const EmptyPage()
        : ListView.builder(
            itemCount: viewModel.datas.length,
            padding: EdgeInsets.zero,
            primary: true,
            itemBuilder: (BuildContext context, int index) {
              return _buildItem(context, viewModel, index);
            });
  }

  Widget _buildItem(BuildContext context, FollowViewModel viewModel, int index) {
    FollowEntity entity = viewModel.datas[index];
    return GestureDetector(
      child: Container(
        padding: const EdgeInsets.only(left: 16, right: 16),
        child: Column(
          children: [
            Container(
              padding: const EdgeInsets.only(top: 16, bottom: 16),
              child: Row(
                children: [
                  ClipRRect(
                    borderRadius: BorderRadius.circular(30),
                    child: Container(
                      color: ColorsUtil.COLOR_DBDBDB,
                      width: 60,
                      height: 60,
                      child: CustomNetworkImage(url: entity.avatar ?? "", fit: BoxFit.cover, logoSize: const Size(22, 22),),
                    ),
                  ),
                  const SizedBox(width: 16),
                  Expanded(
                    child: Text(
                      entity.nickName ?? "",
                      style: const TextStyle(fontSize: 15, fontWeight: FontWeight.w500, color: ColorsUtil.COLOR_333333,),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                  ),
                  CustomGesture(
                    child: Container(
                      padding: const EdgeInsets.only(top: 6, left: 15, bottom: 6, right: 15),
                      decoration: const BoxDecoration(
                        color: ColorsUtil.COLOR_FF5C5C,
                        borderRadius: BorderRadius.all(Radius.circular(6)),
                      ),
                      child: Text(entity.isAttention == 0 ? "关注" : "取消关注", style: const TextStyle(fontSize: 14, color: ColorsUtil.COLOR_FFFFFF)),
                    ),
                    behavior: HitTestBehavior.opaque,
                    onTap: () {
                      if (entity.isAttention == 0) {
                        viewModel.follow(context, entity);
                      } else {
                        viewModel.unfollow(context, entity);
                      }
                    },
                  ),
                ],
              ),
            ),
            index != viewModel.datas.length - 1
                ? Container(height: 0.5, color: ColorsUtil.COLOR_EBEBEB)
                : Container(),
          ],
        ),
      ),
      behavior: HitTestBehavior.opaque,
      onTap: () {},
    );
  }
}
